package com.base.cn.platform.os.service.op.affiche.tmp;

import com.base.cn.platform.os.common.msg.WXTmpEnum;
import com.base.cn.platform.os.common.utils.DateUtils;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.entity.op.affiche.OpReceiveAffiche;
import com.base.cn.platform.os.entity.op.coupon.OpCoupon;
import com.base.cn.platform.os.entity.op.coupon.OpCouponCode;
import com.base.cn.platform.os.entity.user.user.CusUser;
import com.base.cn.platform.os.entity.user.user.CuserProfile;
import org.json.JSONObject;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 赠送优惠券提醒
 *
 * @author s.li
 * @date 2019/05/29
 */
@Component
public class CouponCodeSendTemAffice extends SendTemAffice{
    @Override
    public Map<String, Object> initMsg(Object[] arr, WXTmpEnum tmpEnum, List<CusUser> userList, Map<BigDecimal, List> dataListMap) {
        initService(arr);

        String couponGiveVarNum1 = smsTempConfigMap.get("couponGiveVarNum1");
        String couponGiveVarNum2 = smsTempConfigMap.get("couponGiveVarNum2");
        String couponGiveVarNum3 = smsTempConfigMap.get("couponGiveVarNum3");
        String couponGiveRemark = smsTempConfigMap.get("couponGiveRemark");
        String couponGiveMsgType = smsTempConfigMap.get("couponGiveMsgType");

        if(StringUtils.isNotEmpty(couponGiveVarNum1)
                && StringUtils.isNotEmpty(couponGiveVarNum2)
                && StringUtils.isNotEmpty(couponGiveVarNum3)
                && StringUtils.isNotEmpty(couponGiveMsgType)){
            Map<String,Object> msg = new HashMap<>();
            boolean status = false;
            if(couponGiveMsgType.contains(MsgType.SITE.getKey())){
                List<OpReceiveAffiche> receiveAfficheList = new ArrayList<>();
                userList.forEach(user -> {
                    List<OpCouponCode> couponList = (List<OpCouponCode>)dataListMap.get(user.getId());
                    couponList.forEach(coupon->{
                        OpReceiveAffiche affiche = new OpReceiveAffiche();
                        affiche.setMsgType(1);
                        affiche.setStatus(1);
                        affiche.setUserId(user.getId());
                        StringBuilder feedbackContext = new StringBuilder();
                        feedbackContext.append("赠送优惠券,");
                        feedbackContext.append(couponGiveVarNum1.replace("{1}", user.getNickname()));
                        feedbackContext.append(" , ");
                        feedbackContext.append(couponGiveVarNum2.replace("{2}", coupon.getType()==1?coupon.getLimitAtion()+"折":"立减￥"+coupon.getLimitAtion()));
                        feedbackContext.append(" , ");
                        feedbackContext.append(couponGiveVarNum3.replace("{3}", DateUtils.format(new Date(),"yyyy年MM月dd日 HH:mm")));
                        if(StringUtils.isNotEmpty(couponGiveRemark)){
                            feedbackContext.append(" , ");
                            feedbackContext.append(couponGiveRemark);
                        }
                        affiche.setContext(feedbackContext.toString());
                        receiveAfficheList.add(affiche);
                    });
                });
                msg.put(SITE_MESSAGE_KEY,receiveAfficheList);
                status = true;
            }
            if(couponGiveMsgType.contains(MsgType.WX.getKey())){
                List<CuserProfile> cuserProfileList =this.getCuserProfileList(userList);
                if(ObjectUtils.isNotEmpty(cuserProfileList)){
                    String tmpId = getTmpId(tmpEnum);
                    List<JSONObject> jsonObjectList = new ArrayList<>();
                    Map<BigDecimal,CusUser> userMap = userList.stream().collect(Collectors.toMap(e->e.getId(),e->e));
                    cuserProfileList.forEach(cuserProfile -> {
                        CusUser user = userMap.get(cuserProfile.getUserId());
                        List<OpCouponCode> couponList = (List<OpCouponCode>)dataListMap.get(cuserProfile.getUserId());
                        couponList.forEach(coupon->{
                            //设置参数
                            JSONObject obj = new JSONObject();
                            obj.put("touser", cuserProfile.getOpenId());
                            obj.put("template_id", tmpId);
                            //微信消息数据内容
                            Map<String,Map<String,String>> data = new HashMap<>();

                            //消息标题
                            Map<String,String> first = new HashMap<>();
                            first.put("value","赠送优惠券提醒");
                            first.put("color","#173177");
                            data.put("first",first);

                            Map<String,String> keyword1 = new HashMap<>();
                            keyword1.put("value",user.getNickname());
                            keyword1.put("color","#173177");
                            data.put("keyword1",keyword1);

                            Map<String,String> keyword2 = new HashMap<>();
                            keyword2.put("value", coupon.getType()==1?coupon.getLimitAtion()+"折":"立减￥"+coupon.getLimitAtion());
                            keyword2.put("color","#173177");
                            data.put("keyword2",keyword2);

                            Map<String,String> keyword3 = new HashMap<>();
                            keyword3.put("value", DateUtils.format(new Date(),"yyyy年MM月dd日 HH:mm"));
                            keyword3.put("color","#173177");
                            data.put("keyword3",keyword3);
                            this.setRemarkMap(data,couponGiveRemark);
                            obj.put("data", data);
                            jsonObjectList.add(obj);
                        });
                    });
                    msg.put(WX_MESSAGE_KEY,jsonObjectList);
                    status = true;
                }
            }
            if(status){
                msg.put(MSG_TYPE,couponGiveMsgType);
                return msg;
            }
        }
        return null;
    }
}
